<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 250px;" class="filter-item" size="small"
                placeholder="名称、编号、地址、联系人、联系电话"
                v-model="listQuery.keyword">
      </el-input>
      <el-button class="filter-item" type="primary" size="small" v-waves icon="search" @click="handleFilter">搜索
      </el-button>
      <el-button v-if="sys_dept_add" class="filter-item" size="small" style="margin-left: 10px;" @click="handleCreate"
                 type="primary"
                 icon="edit">添加
      </el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="加载中..." border fit
              highlight-current-row style="width: 99%" size="small">

      <el-table-column align="center" label="序号">
        <template slot-scope="scope">
          <span>{{scope.row.deptId}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="名称">
        <template slot-scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="编号">
        <template slot-scope="scope">
          <span>{{scope.row.deptNo}} </span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="地址">
        <template slot-scope="scope">
          <span>{{scope.row.address}} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系人">
        <template slot-scope="scope">
          <span>{{scope.row.contact}} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系电话">
        <template slot-scope="scope">
          <span>{{scope.row.tel}} </span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{scope.row.createDate | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" class-name="status-col" label="状态">
        <template slot-scope="scope">
          <span>{{scope.row.enable | statusFilter}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" v-if="sys_dept_edit">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.enable" size="small" type="danger" @click="toggle(scope.row)">停用</el-button>
          <el-button v-if="!scope.row.enable" size="small" type="success" @click="toggle(scope.row)">启用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,30,50,100]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item v-if="dialogStatus == 'update'" label="序号" prop="deptId">
          <el-input v-model="form.deptId" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="enable">
          <el-select class="filter-item" v-model="form.enable" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item" :label="item | statusFilter" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">保存</el-button>
        <el-button v-else type="primary" @click="update('form')">更新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {fetchList, getObj, addObj, toggleObj, putObj} from '@/api/dept'
  import waves from "@/directive/waves/index.js"; // 水波纹指令
  import {mapGetters} from 'vuex'

  export default {
    name: 'dept',
    directives: {
      waves
    },
    data() {
      return {
        tableKey: 0,
        list: null,
        dialogStatus: "",
        dialogFormVisible: false,
        textMap: {
          update: "编辑",
          create: "创建"
        },
        statusOptions: [true, false],
        methodOptions: ['GET', 'POST', 'PUT', 'DELETE'],
        total: null,
        listLoading: true,
        listQuery: {
          keyword: undefined,
          page: 1,
          limit: 10
        },
        labelPosition: 'right',
        form: {
          name: undefined,
          deptNo: undefined,
          address: undefined,
          deptId: undefined,
          contact: undefined,
          enable: true,
          tel: undefined
        },
        rules: {
          name: [
            {
              required: true,
              message: "请输入名称",
              trigger: "blur"
            },
            {
              min: 1,
              max: 100,
              message: "长度在 1 到 100 个字符",
              trigger: "blur"
            }
          ],
          contact: [
            {
              required: true,
              message: "请输入联系人",
              trigger: "blur"
            },
            {
              min: 1,
              max: 10,
              message: "长度在10个字符",
              trigger: "blur"
            }
          ],
          enable: [
            {
              required: true,
              message: "请选择状态",
              trigger: "blur"
            }
          ],
          tel: [
            {
              required: true,
              message: "请输入联系电话",
              trigger: "blur"
            },
            {
              min: 7,
              max: 20,
              message: "长度在7-20个字符",
              trigger: "blur"
            }
          ]
        },
        sys_dept_add: false,
        sys_dept_edit: false
      }
    },
    filters: {
      statusFilter(type) {
        const statusMap = {
          true: '启用',
          false: '停用'
        }
        return statusMap[type]
      }
    },
    created() {
      this.getList()
      this.sys_dept_add = this.permissions['sys_dept_add']
      this.sys_dept_edit = this.permissions['sys_dept_edit']
    },
    computed: {
      ...mapGetters([
        'elements',
        'permissions'
      ])
    },
    methods: {
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.list = response.data.data.content;
          this.total = response.data.data.totalElements;
          this.listLoading = false;
        })
      },
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleUpdate(row) {
        getObj(row.deptId).then(response => {
          this.form = response.data.data
          this.dialogFormVisible = true;
          this.dialogStatus = "update";
        })
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = "create";
        this.dialogFormVisible = true;
      },
      update(formName) {
        const set = this.$refs;
        this.form.role = this.role;
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false;
            this.form.password = undefined;
            putObj(this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: "成功",
                message: "修改成功",
                type: "success",
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: "成功",
                message: "创建成功",
                type: "success",
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },
      toggle(row) {
        toggleObj(row.deptId, !row.enable)
          .then(() => {
            this.getList();
            this.$notify({
              title: "成功",
              message: "切换成功",
              type: "success",
              duration: 2000
            });
          });
      },
      cancel(formName) {
        this.dialogFormVisible = false
        this.$refs[formName].resetFields();
      },
      resetForm() {
        this.form = {
          name: undefined,
          deptId: undefined,
          deptNo: undefined,
          address: undefined,
          contact: undefined,
          tel: undefined,
          enable: true
        }
      }
    }
  }
</script>

